package com.sxjh.controller.ny;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sxjh.common.*;
import com.sxjh.entity.Intrude;
import com.sxjh.entity.jxstar.Jxstar;
import com.sxjh.entity.param.IntruduListParam;
import com.sxjh.entity.result.IntruduListResult;
import com.sxjh.service.IntruduService;
import com.sxjh.service.PdfService;
import com.sxjh.uitl.ExcelUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.*;

/**
 * <p>
 * 极星平台
 * </p>
 *
 * @author guanxy
 * @since 2023-06-03
 */
@Slf4j
@Api(tags = "极星平台接口")
@RestController
@RequestMapping
public class StarController extends BaseController {

    @Autowired
    private IntruduService intruduService;

    @Autowired
    private ExcelUtils excelUtils;

    @Autowired
    private PdfService pdfService;

    @Autowired
    private HttpServletResponse response;

    @ApiOperation("报警转发接口")
    @PostMapping(value = "/Callback")
    public Result<Object> create(@RequestBody Jxstar params) {
        log.info("=======接收到极星AI推送消息==========");
        Intrude intrude = new Intrude();
//        Long timeStamp = params.getTime_stamp();
//        LocalDateTime time = LocalDateTime.ofInstant(Instant.ofEpochMilli(timeStamp), ZoneOffset.ofHours(8));
        intrude.setCreatedAt(LocalDateTime.now());
        intrude.setAddr(params.getDevice_name());

        // 解析base64原文件，转成file文件
        String srcpicData = params.getSrcpic_data();

        // 保存到数据库
        intrude.setImg2(params.getSrcpic_url());
        intrude.setIsPush(2);
        intruduService.save(intrude);

        // 异步更新图片信息
        intruduService.getFilePath(srcpicData, intrude.getId());

        HashMap<String, Object> map = new HashMap<>(4);
        map.put("status_code", 1000);
        map.put("message", "回调成功");
        return Result.ok(map);
    }


    @ApiOperation("人员闯入列表接口")
    @PostMapping(value = "/intruduList")
    public Result<Object> intruduList(@RequestBody IntruduListParam param) {
        IPage iPage = new Page(param.getCurrent(), param.getSize());
        if (param.getSize() == -1) {
            iPage.setSize(Constant.maxSize);
        }
        iPage = intruduService.list(iPage, param);
        ZPage<IntruduListResult> zPage = copyPage(iPage, IntruduListResult::new);
        return Result.ok(zPage);
    }

    @PostMapping(value = "intrude/getExport")
    @ApiOperation("人员闯入报表导出excel/pdf")
    public void intrudeExport(@RequestBody IntruduListParam param) {
        param.setSize(-1);
        Result<Object> result = intruduList(param);
        ZPage<IntruduListResult> zPage = (ZPage<IntruduListResult>) result.getData();
        LinkedHashSet<String> headerFields = param.getHeaderFields();

        if (CollUtil.isEmpty(headerFields)) err("导出表头不能为空");
        List<IntruduListResult> records = zPage.getRecords();
        if (CollUtil.isEmpty(records)) err("导出数据不能为空");

        // 操作表头，去掉图片导出
        Iterator<String> iterator = headerFields.iterator();
        while (iterator.hasNext()) {
            String next = iterator.next();
            if ("img1".equals(next)) {
                iterator.remove();
                break;
            }
        }

        // 把导出结果为null的值替换成""空字符串
        for (int i = 0; i < records.size(); i++) {
            IntruduListResult intrude = records.get(i);
            intrude.setIndex(i + 1);
            if (StringUtils.isBlank(intrude.getAddr())) {
                intrude.setAddr("");
            }
            if (StringUtils.isBlank(intrude.getImg1())) {
                intrude.setImg1("");
            }
        }

        Integer flag = param.getFlag();
        if (flag == 1) {
            excelUtils.downLoadFromWeb(response, "人员闯入表", headerFields, records, "模板", IntruduListResult.class);
        } else if (flag == 2) {
            pdfService.getPdf(response, "人员闯入表", headerFields, records, "人员闯入表");
        }
    }

    @ApiOperation("人员闯入已读修改")
    @PostMapping(value = "/receipt")
    public Result<Void> receipt(@RequestBody IntruduListParam param) {
        intruduService.receipt(param.getIds());
        intruduService.pushNotice();
        return Result.ok();
    }

}
